package com.ant.storage.controller;


import com.ant.storage.common.ResultPage;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.GoodsPoolDto;
import com.ant.storage.dto.MergeDto;
import com.ant.storage.pojo.GoodsPool;
import com.ant.storage.service.IAbilityService;
import com.ant.storage.service.IGoodsPoolService;
import com.ant.storage.utils.DingDingUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.taobao.api.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author melon
 * @since 2022-03-17
 */
@RestController
@RequestMapping("/goods-pool")
@Api(tags = "申请池接口")
public class GoodsPoolController {
    @Autowired
    private IGoodsPoolService goodsPoolService;


    @ApiOperation(value = "获取物品库存数据列表")
    @GetMapping("/getGoodsPoolList")
    public ResultPage getGoodsPoolList(GoodsPoolDto goodsPoolDto) {
        return ResultPage.success("获取成功", goodsPoolService.getGoodsPoolList(goodsPoolDto));
    }

    @ApiOperation(value = "通过id获取申请详情")
    @GetMapping("/getGoodsPoolDetailsById/{id}")
    public ResultVo getGoodsPoolDetailsById(@PathVariable("id") Integer id) {
        return ResultVo.success("获取成功", goodsPoolService.getGoodsPoolDetailsById(id));
    }

    @ApiOperation(value = "申请池保存")
    @PostMapping("/saveGoodsPool")
    public ResultVo saveGoodsPool(@RequestBody @Valid GoodsPool goodsPool, BindingResult bindingResult) throws Exception {
        return goodsPoolService.saveGoodsPool(goodsPool, bindingResult);
    }

    @ApiOperation(value = "修改申请池数据")
    @PostMapping("/updateGoodsPool")
    public ResultVo updateGoodsPool(@RequestBody @Valid GoodsPoolDto goodsPoolDto, BindingResult bindingResult) {
        return goodsPoolService.updateGoodsPool(goodsPoolDto, bindingResult);
    }

    @ApiOperation(value = "申请通过申请")
    @GetMapping("/adoptApply/{id}")
    public ResultVo adoptApply(@PathVariable(value = "id", required = false) Integer id) {
        if (id == null) {
            return ResultVo.success("请输入id号");
        } else {
            return goodsPoolService.adoptApply(id);
        }
    }

    @ApiOperation(value = "取消申请")
    @GetMapping("/cancelApply/{id}")
    public ResultVo cancelApply(@PathVariable(value = "id", required = false) Integer id) throws ApiException {
        if (id == null) {
            return ResultVo.success("请输入id号");
        } else {
            GoodsPool pool = goodsPoolService.getOne(new UpdateWrapper<GoodsPool>().eq("id", id));
            if(pool!=null){
                String processInstanceId = pool.getProcessInstanceId();
                String str = DingDingUtil.examineAndApproveRevoke(DingDingUtil.obtainToken(), processInstanceId);
                if(str.equals("0")){
                    goodsPoolService.update(new UpdateWrapper<GoodsPool>().set("status", 1).eq("id", id));
                    return ResultVo.success("操作成功");
                }else{
                    return ResultVo.success("操作失败");
                }
            }else{
                return ResultVo.fail("数据不存在");
            }
        }
    }

    @ApiOperation(value = "小程序查询合并申购详情")
    @GetMapping("/getMergeApply/{ids}")
    public ResultVo getMergeApply(@PathVariable("ids") Integer[] ids) {
        if (ids.length == 0) {
            return ResultVo.success("请输入id号");
        } else {
            return  goodsPoolService.getMergeApply(ids);
        }
    }

    @ApiOperation(value = "小程序完成签收")
    @GetMapping("/signInCompleted/{id}")
    public ResultVo signInCompleted(@PathVariable(value = "id",required = false)  Integer  id) {
        if (id == null) {
            return ResultVo.success("请输入id号");
        } else {
            return  goodsPoolService.signInCompleted(id);
        }
    }

    @ApiOperation(value = "合并申购")
    @PostMapping("/mergeApply")
    public ResultVo mergeApply(@RequestBody MergeDto dto) {
        if (dto.getId().size() == 0) {
            return ResultVo.success("请输入id号");
        } else {
            return goodsPoolService.mergeApply(dto);
        }
    }


    @ApiOperation(value = "获取物品库存数据列表")
    @GetMapping("/Applets/getAppletsGoodsPoolList")
    public ResultPage getAppletsGoodsPoolList(GoodsPoolDto goodsPoolDto) {
        return ResultPage.success("获取成功", goodsPoolService.getAppletsGoodsPoolList(goodsPoolDto));
    }


    @ApiOperation(value = "分配申请单")
    @GetMapping("/getDistributionGoodsPool/{nameId}/{ids}")
    public ResultVo getDistributionGoodsPool(@PathVariable("nameId") Integer nameId, @PathVariable("ids") Integer[] ids) {
        return  goodsPoolService.getDistributionGoodsPool(nameId,ids);
    }




    @ApiOperation(value = "小程序获取申请记录")
    @GetMapping("/getAppletGoodsPoolList")
    public ResultPage getAppletGoodsPoolList(GoodsPoolDto goodsPoolDto) {
        return ResultPage.success("获取成功", goodsPoolService.getAppletGoodsPoolList(goodsPoolDto));
    }

    @ApiOperation(value = "通过id获取申请详情")
    @GetMapping("/getAppletGoodsPoolDetailsById/{id}")
    public ResultVo getAppletGoodsPoolDetailsById(@PathVariable("id") Integer id) {
        return ResultVo.success("获取成功", goodsPoolService.getAppletGoodsPoolDetailsById(id));
    }
}
